﻿namespace RankDemo.WebApi.Entities
{
    public class Customer
    {
        private const int MaxScoreValue = 1000;
        private const int MinScoreValue = -1000;
        private const int DefaultScoreValue = 0;

        public Customer()
        {
            Score = DefaultScoreValue;
        }

        public long CustomerId { get; private set; }
        public int Score { get; private set; }

        public Customer SetCustomerId(long customerId)
        {
            if (customerId < 0)
            {
                throw new ArgumentException("Customer id should not be less than 0");
            }

            CustomerId = customerId;
            return this;
        }

        public Customer ChangeScore(int score)
        {
            if (score < MinScoreValue || score > MaxScoreValue)
            {
                throw new ArgumentOutOfRangeException(nameof(score), $"The given score value exceeds the current range, and the current allowable range is [{MinScoreValue},{MaxScoreValue}]");
            }

            if (Score == DefaultScoreValue && score <= DefaultScoreValue)
            {
                throw new ArgumentOutOfRangeException(nameof(score), "Exceeds the current range of existing scores");
            }

            Score += score;

            if (Score < DefaultScoreValue)
            {
                Score = DefaultScoreValue;
            }

            return this;
        }

        public bool IsDefaultScoreValue()
        {
            return Score == DefaultScoreValue;
        }
    }
}
